package ${package.Controller};

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import cn.hellochaos.generator.entity.dto.ResultCode;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import ${package.Entity}.dto.ResultBean;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import javax.annotation.Resource;


/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 * @version v1.0
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@CrossOrigin
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/api/v1/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};

    /**
    后面将swagger注解里的  春眠不觉晓  替换为实体类名
    * 查询分页数据
    */
    @ApiOperation("分页查询所有数据")
    @ApiImplicitParams({
    @ApiImplicitParam(name="page",value="页号",required=false,paramType="query",dataType="Integer"),
    @ApiImplicitParam(name="pageSize",value="页大小",required=false,paramType="query",dataType="Integer"),
    @ApiImplicitParam(name="factor",value="条件查询值（根据条件后端可以修改,默认不传）",required=false,paramType="query",dataType="String")
    })
    @RequestMapping(method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor) {
        return new ResultBean<>(${table.serviceName?uncap_first}.list${entity}sByPage(page, pageSize,factor));
    }

    /**
    * 根据id查询
    */
    @ApiOperation("根据id查询春眠不觉晓数据")
    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value="id的值",required=true,paramType="path",dataType="Integer")
    })
    @RequestMapping(method = RequestMethod.GET, value = "/{id}",produces = "application/json;charset=UTF-8")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {

        ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.get${entity}ById(id);
        if (${entity?uncap_first}==null){
        return new ResultBean<>(ResultCode.MSG_RESOURCE_NOT_FOUND,ResultCode.RESOURCE_NOT_FOUND,null);
        }
        return new ResultBean<>(${entity?uncap_first});
    }

    /**
    * 新增
    */
    @ApiOperation("新增一条春眠不觉晓数据")
    @RequestMapping(method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ResultBean<?> insert(@RequestBody ${entity} ${entity?uncap_first}) {
        return new ResultBean<>(${table.serviceName?uncap_first}.insert${entity}(${entity?uncap_first}));
    }

    /**
    * 删除
    */
    @ApiOperation("删除一条春眠不觉晓数据")
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}",produces = "application/json;charset=UTF-8")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {

    int deletedId = ${table.serviceName?uncap_first}.delete${entity}ById(id);

    if (deletedId==-1){
    return new ResultBean<>(ResultCode.MSG_RESOURCE_NOT_FOUND,ResultCode.RESOURCE_NOT_FOUND,null);
    }
    return new ResultBean<>(deletedId);
    }

    /**
    * 修改
    */
    @ApiOperation("更新一条春眠不觉晓数据")
    @RequestMapping(method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    public ResultBean<?> updateById(@RequestBody ${entity} ${entity?uncap_first}) {
        int updatedId = ${table.serviceName?uncap_first}.update${entity}(${entity?uncap_first});

        if (updatedId==-1){
            return new ResultBean<>(ResultCode.MSG_RESOURCE_NOT_FOUND,ResultCode.RESOURCE_NOT_FOUND,null);
        }
        return new ResultBean<>(updatedId);
    }
}
</#if>